package com.examples.calendar;

import java.util.Calendar;
import java.util.Date;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

public class CustomPaintListener implements PaintListener {
	
	Display display;
	
	Rectangle defaultRectangle;

	public CustomPaintListener(Display display) {
		this.display = display;
	}

	public Rectangle getRectangle(){
		return defaultRectangle;
	}

	@Override
	public void paintControl(PaintEvent e) {

		final Rectangle rectangle = display.getActiveShell().getClientArea();
		
		final int totalWidth = rectangle.width;
		final int totalHeight = rectangle.height;
		final int widthOfCell = (totalWidth - 5) / 7;
		final int heightOfCell = (totalHeight - 20) / 5;
		
		GC gc = e.gc;
		gc.drawRectangle(rectangle.x, rectangle.y, totalWidth, totalHeight);
		
		gc.drawText(Days.SUNDAY.toString(), widthOfCell / 4 + 20, 5);
		gc.drawText(Days.MONDAY.toString(), widthOfCell + widthOfCell / 4 + 20, 5);
		gc.drawText(Days.TUESDAY.toString(), widthOfCell * 2 + widthOfCell / 4 + 20, 5);
		gc.drawText(Days.WEDNESDAY.toString(), widthOfCell * 3 + widthOfCell / 4 + 20, 5);
		gc.drawText(Days.THURSDAY.toString(), widthOfCell * 4 + widthOfCell / 4 + 20, 5);
		gc.drawText(Days.FRIDAY.toString(), widthOfCell * 5 + widthOfCell / 4 + 20, 5);
		gc.drawText(Days.SATURDAY.toString(), widthOfCell * 6 + widthOfCell / 4 + 20, 5);
		
		for (int i = 0; i < 6; i++) {
			gc.drawRectangle(i * widthOfCell, 0, widthOfCell, 20);
			for (int j = 0; j < 5; j++) {
				gc.drawRectangle(i * widthOfCell, j * heightOfCell + 20, widthOfCell, heightOfCell);
			}
		}
		gc.drawRectangle(6 * widthOfCell, 0, widthOfCell, 20);
		for(int i = 0; i < 5; i ++){
			gc.drawRectangle(6 * widthOfCell, heightOfCell * i + 20, widthOfCell, heightOfCell);
		}
		
		gc.setBackground(new Color(display, new RGB(0, 220, 0)));
		
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		
		int j = 0;
			for (int i = 1; i < 32; i++) {
				int mod = (i + 1) % (7);
					if(mod == 0){
						j++;
						gc.setBackground(new Color(display, new RGB(220, 100, 100)));
						gc.setForeground(new Color(display, 0, 0, 0));
						if(i == 6){
							gc.drawText(" "+i+" ", 1 * widthOfCell - 13, j * heightOfCell + 21);
						}
						else{
							gc.drawText(""+i, 1 * widthOfCell - 13, j * heightOfCell + 21);
						}
					}
					else{
						if(j == 0) {
							gc.drawText(" "+i+" ", (mod + 1) * widthOfCell - 13, j * heightOfCell + 21);
						}
						else {
							gc.setForeground(new Color(display, 0, 0, 0));
							gc.setBackground(new Color(display, new RGB(0, 220, 0)));
							if(i == 7 || i == 8 || i == 9){
								gc.drawText(" "+i+" ", (mod + 1) * widthOfCell - 13, j * heightOfCell + 21);	
							}
							else{
								gc.drawText(""+i, (mod + 1) * widthOfCell - 13, j * heightOfCell + 21);
							}
						}
					}
				if(i == c.get(Calendar.DAY_OF_MONTH)){
					gc.setForeground(new Color(display, 255, 0, 0));
					gc.drawRectangle(mod * widthOfCell + 1, j * heightOfCell + 21, widthOfCell - 1, heightOfCell - 1);
					defaultRectangle = new Rectangle(mod * widthOfCell + 1, j * heightOfCell + 21, widthOfCell - 1, heightOfCell - 1);
					RectangleHolder.setRectangle(defaultRectangle);
				}
			}	
	}
}
